#include "stdafx.h"
#include "Mainform.h"

using namespace particleEditorCli;

Mainform::Mainform(void):
							mOgreinit(0),
							mRWMan(nullptr)
{
	InitializeComponent();
	mInstance = this;

	mOgreinit = new OgreInit();
	if(!mOgreinit->Init())
		throw gcnew System::Exception("Could not initialize ogre!");
	
	//Managers
	gcnew RenderWindowManager();
	gcnew ParticleManagerForm();
	gcnew ConsoleForm();
	gcnew PropertyBoxForm();
	
	if(System::IO::File::Exists("Layout.xml"))
	{
		mDockPanel->LoadFromXml("Layout.xml", gcnew WeifenLuo::WinFormsUI::Docking::DeserializeDockContent(this, &Mainform::loadLayoutCallback));
	}
	else
	{
		ConsoleForm::Instance->Show(mDockPanel);
		RenderWindowManager::Instance->addRenderWindow();
		ParticleManagerForm::Instance->Show(mDockPanel);
		PropertyBoxForm::Instance->Show(mDockPanel);
	}

	Shown += gcnew EventHandler(this, &Mainform::OnShownCallback);
}

Mainform::~Mainform()
{
	if(RenderWindowManager::Instance!=nullptr)
	{
		RenderWindowManager::Instance->stopRendering();
		delete RenderWindowManager::Instance;
	}

	if(mOgreinit)
	{
		delete mOgreinit;
	}
	
	if (components)
	{
		delete components;
	}
}

void Mainform::OnShownCallback(Object^ sender, EventArgs^e)
{
	RenderWindowManager::Instance->startRendering();
}

IDockContent^ Mainform::loadLayoutCallback(System::String^ str)
{
	if(str=="particleEditorCli.ConsoleForm")
		return ConsoleForm::Instance;
	if(str=="particleEditorCli.ParticleManagerForm")
		return ParticleManagerForm::Instance;
	if(str=="particleEditorCli.RenderWindowForm")
		return RenderWindowManager::Instance->addRenderWindow();
	if(str=="particleEditorCli.PropertyBoxForm")
		return PropertyBoxForm::Instance;
	
	return nullptr;
}

Mainform^ Mainform::Instance::get()
{
	return mInstance;
}

WeifenLuo::WinFormsUI::Docking::DockPanel^ Mainform::Dockpanel::get()
{
	return mDockPanel;
}

System::Void Mainform::mToolStripMenuItemLoad_Click(System::Object^  sender, System::EventArgs^  e)
{
	if(System::IO::File::Exists("Layout.xml"))
	{
		RenderWindowManager::Instance->removeAllRenderWindow();
		ParticleManagerForm::Instance->DockPanel = nullptr;
		ConsoleForm::Instance->DockPanel = nullptr;
		PropertyBoxForm::Instance->DockPanel = nullptr;
		mDockPanel->LoadFromXml("Layout.xml", gcnew WeifenLuo::WinFormsUI::Docking::DeserializeDockContent(this, &Mainform::loadLayoutCallback));
	}
}

System::Void Mainform::mToolStripMenuItemSave_Click(System::Object^  sender, System::EventArgs^  e)
{
	mDockPanel->SaveAsXml("Layout.xml");
}

System::Void Mainform::mToolStripMenuItemConsole_Click(System::Object^  sender, System::EventArgs^  e)
{
	ConsoleForm::Instance->Show(mDockPanel);
}

System::Void Mainform::mToolStripMenuItemPropertyBox_Click(System::Object^  sender, System::EventArgs^  e)
{
	PropertyBoxForm::Instance->Show(mDockPanel);
}

System::Void Mainform::mToolStripMenuItemParticleManager_Click(System::Object^  sender, System::EventArgs^  e)
{
	ParticleManagerForm::Instance->Show(mDockPanel);
}

System::Void Mainform::mToolStripMenuItemRenderBox_Click(System::Object^  sender, System::EventArgs^  e)
{
	RenderWindowManager::Instance->addRenderWindow();
}


void Mainform::InitializeComponent(void)
{
	System::ComponentModel::ComponentResourceManager^  resources = (gcnew System::ComponentModel::ComponentResourceManager(Mainform::typeid));
	WeifenLuo::WinFormsUI::Docking::DockPanelSkin^  dockPanelSkin6 = (gcnew WeifenLuo::WinFormsUI::Docking::DockPanelSkin());
	WeifenLuo::WinFormsUI::Docking::AutoHideStripSkin^  autoHideStripSkin6 = (gcnew WeifenLuo::WinFormsUI::Docking::AutoHideStripSkin());
	WeifenLuo::WinFormsUI::Docking::DockPanelGradient^  dockPanelGradient16 = (gcnew WeifenLuo::WinFormsUI::Docking::DockPanelGradient());
	WeifenLuo::WinFormsUI::Docking::TabGradient^  tabGradient36 = (gcnew WeifenLuo::WinFormsUI::Docking::TabGradient());
	WeifenLuo::WinFormsUI::Docking::DockPaneStripSkin^  dockPaneStripSkin6 = (gcnew WeifenLuo::WinFormsUI::Docking::DockPaneStripSkin());
	WeifenLuo::WinFormsUI::Docking::DockPaneStripGradient^  dockPaneStripGradient6 = (gcnew WeifenLuo::WinFormsUI::Docking::DockPaneStripGradient());
	WeifenLuo::WinFormsUI::Docking::TabGradient^  tabGradient37 = (gcnew WeifenLuo::WinFormsUI::Docking::TabGradient());
	WeifenLuo::WinFormsUI::Docking::DockPanelGradient^  dockPanelGradient17 = (gcnew WeifenLuo::WinFormsUI::Docking::DockPanelGradient());
	WeifenLuo::WinFormsUI::Docking::TabGradient^  tabGradient38 = (gcnew WeifenLuo::WinFormsUI::Docking::TabGradient());
	WeifenLuo::WinFormsUI::Docking::DockPaneStripToolWindowGradient^  dockPaneStripToolWindowGradient6 = (gcnew WeifenLuo::WinFormsUI::Docking::DockPaneStripToolWindowGradient());
	WeifenLuo::WinFormsUI::Docking::TabGradient^  tabGradient39 = (gcnew WeifenLuo::WinFormsUI::Docking::TabGradient());
	WeifenLuo::WinFormsUI::Docking::TabGradient^  tabGradient40 = (gcnew WeifenLuo::WinFormsUI::Docking::TabGradient());
	WeifenLuo::WinFormsUI::Docking::DockPanelGradient^  dockPanelGradient18 = (gcnew WeifenLuo::WinFormsUI::Docking::DockPanelGradient());
	WeifenLuo::WinFormsUI::Docking::TabGradient^  tabGradient41 = (gcnew WeifenLuo::WinFormsUI::Docking::TabGradient());
	WeifenLuo::WinFormsUI::Docking::TabGradient^  tabGradient42 = (gcnew WeifenLuo::WinFormsUI::Docking::TabGradient());
	this->mStatusStrip = (gcnew System::Windows::Forms::StatusStrip());
	this->mToolStrip = (gcnew System::Windows::Forms::ToolStrip());
	this->newToolStripButton = (gcnew System::Windows::Forms::ToolStripButton());
	this->openToolStripButton = (gcnew System::Windows::Forms::ToolStripButton());
	this->saveToolStripButton = (gcnew System::Windows::Forms::ToolStripButton());
	this->mMenuStrip = (gcnew System::Windows::Forms::MenuStrip());
	this->mToolStripMenuItemFile = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mDockPanel = (gcnew WeifenLuo::WinFormsUI::Docking::DockPanel());
	this->mToolStripMenuItemEdit = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->viewToolStripMenuItem = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStripMenuItemLayout = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStripMenuItemLoad = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStripMenuItemSave = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->toolStripSeparator1 = (gcnew System::Windows::Forms::ToolStripSeparator());
	this->mToolStripMenuItemWindows = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStripMenuItemConsole = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStripMenuItemPropertyBox = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStripMenuItemRenderBox = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStripMenuItemParticleManager = (gcnew System::Windows::Forms::ToolStripMenuItem());
	this->mToolStrip->SuspendLayout();
	this->mMenuStrip->SuspendLayout();
	this->SuspendLayout();
	// 
	// mStatusStrip
	// 
	this->mStatusStrip->Location = System::Drawing::Point(0, 540);
	this->mStatusStrip->Name = L"mStatusStrip";
	this->mStatusStrip->Size = System::Drawing::Size(784, 22);
	this->mStatusStrip->TabIndex = 0;
	this->mStatusStrip->Text = L"statusStrip1";
	// 
	// mToolStrip
	// 
	this->mToolStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->newToolStripButton, 
		this->openToolStripButton, this->saveToolStripButton});
	this->mToolStrip->Location = System::Drawing::Point(0, 24);
	this->mToolStrip->Name = L"mToolStrip";
	this->mToolStrip->Size = System::Drawing::Size(784, 25);
	this->mToolStrip->TabIndex = 1;
	this->mToolStrip->Text = L"toolStrip1";
	// 
	// newToolStripButton
	// 
	this->newToolStripButton->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
	this->newToolStripButton->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"newToolStripButton.Image")));
	this->newToolStripButton->ImageTransparentColor = System::Drawing::Color::Magenta;
	this->newToolStripButton->Name = L"newToolStripButton";
	this->newToolStripButton->Size = System::Drawing::Size(23, 22);
	this->newToolStripButton->Text = L"&New";
	// 
	// openToolStripButton
	// 
	this->openToolStripButton->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
	this->openToolStripButton->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"openToolStripButton.Image")));
	this->openToolStripButton->ImageTransparentColor = System::Drawing::Color::Magenta;
	this->openToolStripButton->Name = L"openToolStripButton";
	this->openToolStripButton->Size = System::Drawing::Size(23, 22);
	this->openToolStripButton->Text = L"&Open";
	// 
	// saveToolStripButton
	// 
	this->saveToolStripButton->DisplayStyle = System::Windows::Forms::ToolStripItemDisplayStyle::Image;
	this->saveToolStripButton->Image = (cli::safe_cast<System::Drawing::Image^  >(resources->GetObject(L"saveToolStripButton.Image")));
	this->saveToolStripButton->ImageTransparentColor = System::Drawing::Color::Magenta;
	this->saveToolStripButton->Name = L"saveToolStripButton";
	this->saveToolStripButton->Size = System::Drawing::Size(23, 22);
	this->saveToolStripButton->Text = L"&Save";
	// 
	// mMenuStrip
	// 
	this->mMenuStrip->Items->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->mToolStripMenuItemFile, 
		this->mToolStripMenuItemEdit, this->viewToolStripMenuItem});
	this->mMenuStrip->Location = System::Drawing::Point(0, 0);
	this->mMenuStrip->Name = L"mMenuStrip";
	this->mMenuStrip->Size = System::Drawing::Size(784, 24);
	this->mMenuStrip->TabIndex = 2;
	this->mMenuStrip->Text = L"menuStrip1";
	// 
	// mToolStripMenuItemFile
	// 
	this->mToolStripMenuItemFile->Name = L"mToolStripMenuItemFile";
	this->mToolStripMenuItemFile->Size = System::Drawing::Size(37, 20);
	this->mToolStripMenuItemFile->Text = L"File";
	// 
	// mDockPanel
	// 
	this->mDockPanel->ActiveAutoHideContent = nullptr;
	this->mDockPanel->BackColor = System::Drawing::SystemColors::ControlDark;
	this->mDockPanel->Dock = System::Windows::Forms::DockStyle::Fill;
	this->mDockPanel->DockBackColor = System::Drawing::SystemColors::ControlDark;
	this->mDockPanel->DocumentStyle = WeifenLuo::WinFormsUI::Docking::DocumentStyle::DockingWindow;
	this->mDockPanel->Location = System::Drawing::Point(0, 49);
	this->mDockPanel->Name = L"mDockPanel";
	this->mDockPanel->Size = System::Drawing::Size(784, 491);
	dockPanelGradient16->EndColor = System::Drawing::SystemColors::ControlLight;
	dockPanelGradient16->StartColor = System::Drawing::SystemColors::ControlLight;
	autoHideStripSkin6->DockStripGradient = dockPanelGradient16;
	tabGradient36->EndColor = System::Drawing::SystemColors::Control;
	tabGradient36->StartColor = System::Drawing::SystemColors::Control;
	tabGradient36->TextColor = System::Drawing::SystemColors::ControlDarkDark;
	autoHideStripSkin6->TabGradient = tabGradient36;
	autoHideStripSkin6->TextFont = (gcnew System::Drawing::Font(L"Segoe UI", 9));
	dockPanelSkin6->AutoHideStripSkin = autoHideStripSkin6;
	tabGradient37->EndColor = System::Drawing::SystemColors::ControlLightLight;
	tabGradient37->StartColor = System::Drawing::SystemColors::ControlLightLight;
	tabGradient37->TextColor = System::Drawing::SystemColors::ControlText;
	dockPaneStripGradient6->ActiveTabGradient = tabGradient37;
	dockPanelGradient17->EndColor = System::Drawing::SystemColors::Control;
	dockPanelGradient17->StartColor = System::Drawing::SystemColors::Control;
	dockPaneStripGradient6->DockStripGradient = dockPanelGradient17;
	tabGradient38->EndColor = System::Drawing::SystemColors::ControlLight;
	tabGradient38->StartColor = System::Drawing::SystemColors::ControlLight;
	tabGradient38->TextColor = System::Drawing::SystemColors::ControlText;
	dockPaneStripGradient6->InactiveTabGradient = tabGradient38;
	dockPaneStripSkin6->DocumentGradient = dockPaneStripGradient6;
	dockPaneStripSkin6->TextFont = (gcnew System::Drawing::Font(L"Segoe UI", 9));
	tabGradient39->EndColor = System::Drawing::SystemColors::ActiveCaption;
	tabGradient39->LinearGradientMode = System::Drawing::Drawing2D::LinearGradientMode::Vertical;
	tabGradient39->StartColor = System::Drawing::SystemColors::GradientActiveCaption;
	tabGradient39->TextColor = System::Drawing::SystemColors::ActiveCaptionText;
	dockPaneStripToolWindowGradient6->ActiveCaptionGradient = tabGradient39;
	tabGradient40->EndColor = System::Drawing::SystemColors::Control;
	tabGradient40->StartColor = System::Drawing::SystemColors::Control;
	tabGradient40->TextColor = System::Drawing::SystemColors::ControlText;
	dockPaneStripToolWindowGradient6->ActiveTabGradient = tabGradient40;
	dockPanelGradient18->EndColor = System::Drawing::SystemColors::ControlLight;
	dockPanelGradient18->StartColor = System::Drawing::SystemColors::ControlLight;
	dockPaneStripToolWindowGradient6->DockStripGradient = dockPanelGradient18;
	tabGradient41->EndColor = System::Drawing::SystemColors::InactiveCaption;
	tabGradient41->LinearGradientMode = System::Drawing::Drawing2D::LinearGradientMode::Vertical;
	tabGradient41->StartColor = System::Drawing::SystemColors::GradientInactiveCaption;
	tabGradient41->TextColor = System::Drawing::SystemColors::InactiveCaptionText;
	dockPaneStripToolWindowGradient6->InactiveCaptionGradient = tabGradient41;
	tabGradient42->EndColor = System::Drawing::Color::Transparent;
	tabGradient42->StartColor = System::Drawing::Color::Transparent;
	tabGradient42->TextColor = System::Drawing::SystemColors::ControlDarkDark;
	dockPaneStripToolWindowGradient6->InactiveTabGradient = tabGradient42;
	dockPaneStripSkin6->ToolWindowGradient = dockPaneStripToolWindowGradient6;
	dockPanelSkin6->DockPaneStripSkin = dockPaneStripSkin6;
	this->mDockPanel->Skin = dockPanelSkin6;
	this->mDockPanel->TabIndex = 3;
	// 
	// mToolStripMenuItemEdit
	// 
	this->mToolStripMenuItemEdit->Name = L"mToolStripMenuItemEdit";
	this->mToolStripMenuItemEdit->Size = System::Drawing::Size(39, 20);
	this->mToolStripMenuItemEdit->Text = L"Edit";
	// 
	// viewToolStripMenuItem
	// 
	this->viewToolStripMenuItem->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(4) {this->mToolStripMenuItemLayout, 
		this->toolStripSeparator1, this->mToolStripMenuItemWindows, this->mToolStripMenuItemRenderBox});
	this->viewToolStripMenuItem->Name = L"viewToolStripMenuItem";
	this->viewToolStripMenuItem->Size = System::Drawing::Size(44, 20);
	this->viewToolStripMenuItem->Text = L"View";
	// 
	// mToolStripMenuItemLayout
	// 
	this->mToolStripMenuItemLayout->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(2) {this->mToolStripMenuItemLoad, 
		this->mToolStripMenuItemSave});
	this->mToolStripMenuItemLayout->Name = L"mToolStripMenuItemLayout";
	this->mToolStripMenuItemLayout->Size = System::Drawing::Size(152, 22);
	this->mToolStripMenuItemLayout->Text = L"Layout";
	// 
	// mToolStripMenuItemLoad
	// 
	this->mToolStripMenuItemLoad->Name = L"mToolStripMenuItemLoad";
	this->mToolStripMenuItemLoad->Size = System::Drawing::Size(152, 22);
	this->mToolStripMenuItemLoad->Text = L"Load";
	this->mToolStripMenuItemLoad->Click += gcnew System::EventHandler(this, &Mainform::mToolStripMenuItemLoad_Click);
	// 
	// mToolStripMenuItemSave
	// 
	this->mToolStripMenuItemSave->Name = L"mToolStripMenuItemSave";
	this->mToolStripMenuItemSave->Size = System::Drawing::Size(152, 22);
	this->mToolStripMenuItemSave->Text = L"Save";
	this->mToolStripMenuItemSave->Click += gcnew System::EventHandler(this, &Mainform::mToolStripMenuItemSave_Click);
	// 
	// toolStripSeparator1
	// 
	this->toolStripSeparator1->Name = L"toolStripSeparator1";
	this->toolStripSeparator1->Size = System::Drawing::Size(149, 6);
	// 
	// mToolStripMenuItemWindows
	// 
	this->mToolStripMenuItemWindows->DropDownItems->AddRange(gcnew cli::array< System::Windows::Forms::ToolStripItem^  >(3) {this->mToolStripMenuItemConsole, 
		this->mToolStripMenuItemPropertyBox, this->mToolStripMenuItemParticleManager});
	this->mToolStripMenuItemWindows->Name = L"mToolStripMenuItemWindows";
	this->mToolStripMenuItemWindows->Size = System::Drawing::Size(152, 22);
	this->mToolStripMenuItemWindows->Text = L"Windows";
	// 
	// mToolStripMenuItemConsole
	// 
	this->mToolStripMenuItemConsole->Name = L"mToolStripMenuItemConsole";
	this->mToolStripMenuItemConsole->Size = System::Drawing::Size(163, 22);
	this->mToolStripMenuItemConsole->Text = L"Console";
	this->mToolStripMenuItemConsole->Click += gcnew System::EventHandler(this, &Mainform::mToolStripMenuItemConsole_Click);
	// 
	// mToolStripMenuItemPropertyBox
	// 
	this->mToolStripMenuItemPropertyBox->Name = L"mToolStripMenuItemPropertyBox";
	this->mToolStripMenuItemPropertyBox->Size = System::Drawing::Size(163, 22);
	this->mToolStripMenuItemPropertyBox->Text = L"Property box";
	this->mToolStripMenuItemPropertyBox->Click += gcnew System::EventHandler(this, &Mainform::mToolStripMenuItemPropertyBox_Click);
	// 
	// mToolStripMenuItemRenderBox
	// 
	this->mToolStripMenuItemRenderBox->Name = L"mToolStripMenuItemRenderBox";
	this->mToolStripMenuItemRenderBox->Size = System::Drawing::Size(152, 22);
	this->mToolStripMenuItemRenderBox->Text = L"RenderBox";
	this->mToolStripMenuItemRenderBox->Click += gcnew System::EventHandler(this, &Mainform::mToolStripMenuItemRenderBox_Click);
	// 
	// mToolStripMenuItemParticleManager
	// 
	this->mToolStripMenuItemParticleManager->Name = L"mToolStripMenuItemParticleManager";
	this->mToolStripMenuItemParticleManager->Size = System::Drawing::Size(163, 22);
	this->mToolStripMenuItemParticleManager->Text = L"Particle Manager";
	this->mToolStripMenuItemParticleManager->Click += gcnew System::EventHandler(this, &Mainform::mToolStripMenuItemParticleManager_Click);
	// 
	// Mainform
	// 
	this->AutoScaleDimensions = System::Drawing::SizeF(6, 13);
	this->AutoScaleMode = System::Windows::Forms::AutoScaleMode::Font;
	this->ClientSize = System::Drawing::Size(784, 562);
	this->Controls->Add(this->mDockPanel);
	this->Controls->Add(this->mToolStrip);
	this->Controls->Add(this->mStatusStrip);
	this->Controls->Add(this->mMenuStrip);
	this->MainMenuStrip = this->mMenuStrip;
	this->Name = L"Mainform";
	this->Text = L"ParticleEditor";
	this->mToolStrip->ResumeLayout(false);
	this->mToolStrip->PerformLayout();
	this->mMenuStrip->ResumeLayout(false);
	this->mMenuStrip->PerformLayout();
	this->ResumeLayout(false);
	this->PerformLayout();

}



[STAThreadAttribute]
int main(array<System::String ^> ^args)
{
	// Enabling Windows XP visual effects before any controls are created
	Application::EnableVisualStyles();
	Application::SetCompatibleTextRenderingDefault(false); 

	// Create the main window and run it
	Application::Run(gcnew Mainform());
	return 0;
}
